import { MockMethod } from 'vite-plugin-mock'

const list = [
  { id: '1', name: 'vben', age: 18, isShow: true },
  { id: '2', name: 'hisd', age: 18, isShow: true },
  { id: '3', name: 'sdcnkevw', age: 18, isShow: true },
  { id: '4', name: '3rjtyh', age: 18, isShow: true },
  { id: '5', name: 'asdeher', age: 18, isShow: true },
  { id: '6', name: 'dbthw', age: 18, isShow: true },
  { id: '7', name: 'qsbc', age: 18, isShow: true },
  { id: '8', name: 'sdvu6', age: 18, isShow: true },
  { id: '9', name: 'cxbuuy', age: 18, isShow: true },
  { id: '10', name: 'bsxghtj', age: 18, isShow: true }
]

export interface Ilist {
  id: string
  name: string
  age: number,
  isShow: boolean
}
export interface IResponseGetUser<T> {
  code: number
  data: T
  total?: number
}

export default [
  {
    url: '/api/ff',
    method: 'get',
    response: () => {
      return {
        code: 200,
        data: 123
      }
    }
  },
  {
    url: '/api/getUsers',
    method: 'get',
    response: ({ query }: any) => {
      const { page, limit } = query
      const data = list.filter(_ => _.isShow).filter((_, index) => {
        return index < page * limit && index >= (page - 1) * limit
      })

      return {
        code: 200,
        data,
        total: list.length
      }
    }
  },
  {
    url: '/api/deleteUser',
    method: 'get',
    response: ({ query }: any) => {
      const { id } = query
      const data = list.find(item => item.id === id)

      data && (data.isShow = false)

      return {
        code: 200,
        data,
        total: list.length
      }
    }
  },
  {
    url: '/api/getUser',
    method: 'get',
    response: ({ query }: any): IResponseGetUser<Ilist | undefined> => {
      const { id } = query
      const data = list.find((item) => item.id === id)
      return {
        code: 200,
        data
      }
    }
  },
  {
    url: '/api/updateUser',
    method: 'post',
    response: ({ body }: any): IResponseGetUser<Ilist | undefined> => {
      const { id, data } = body
      const item = list.find(item => item.id === id)
      if (item) {
        item.name = data.name
        item.age = Number(data.age)
      }
      return {
        code: 200,
        data: item,
        total: list.length
      }
    }
  },
  {
    url: '/api/createUser',
    method: 'post',
    response: ({ body }: any): IResponseGetUser<Ilist | undefined> => {
      const { name, age } = body
      const id = list.length + 1 + ''
      const item: Ilist = {
        id,
        name,
        age,
        isShow: true
      }

      list.push(item)

      return {
        code: 200,
        data: item,
        total: list.length
      }
    }
  }
] as MockMethod[]